package com.cyl.XmlDemo.listener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;

import com.cyl.XmlDemo.entity.DMsg;
import com.cyl.XmlDemo.entity.RoomInfo;
import com.cyl.XmlDemo.entity.RoomList;
import com.cyl.XmlDemo.entity.ShiroPrincipal;
import com.cyl.XmlDemo.entity.UserInfo;
import com.cyl.XmlDemo.entity.UserList;
import com.cyl.XmlDemo.service.MsgService;
import com.cyl.XmlDemo.utils.ShiroUtils;

@Component
public class SessionSubscribeListener implements ApplicationListener<SessionSubscribeEvent> {
	
	private static final Logger logger = LoggerFactory.getLogger(SessionSubscribeListener.class);
	
	@Autowired
	private RoomList roomList;
	
	@Autowired
	private UserList userList;
	
	@Autowired
	private MsgService msgService;

	@Override
	public void onApplicationEvent(SessionSubscribeEvent event) {
		// TODO Auto-generated method stub
		StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
		//处理加入房间事件
		if(!headerAccessor.getDestination().contains("message")){
			//游客直接使用sessionid当作用户名
			String uid = null;
//			Subject subject = SecurityUtils.getSubject();
//			if(subject != null){
//				uid = subject.getPrincipal().getUser().getUsername();
//			}else{
//				uid = headerAccessor.getSessionAttributes().get("sessionId").toString();
//			}
			uid = headerAccessor.getSessionAttributes().get("sessionId").toString();
			String dest = headerAccessor.getDestination().substring(7);
			//arg[0] 房间类型 ；arg[1] 房间号
			String[] arg = dest.split("-");
			if(roomList.contain(arg[1])){
				//已存在房间，用户直接加入房间
				RoomInfo roomInfo = roomList.getRoom(arg[1]);
				//用户加入房间
				if(!roomInfo.containUser(uid)){
					roomInfo.addUser(uid);
				}
				
				//存储用户操作信息
				UserInfo userInfo = new UserInfo(arg[1]);
				userList.addUser(uid, userInfo);

			}else{
				//房间不存在，需要先创建房间
				
				RoomInfo roomInfo = new RoomInfo(arg[0],arg[1],uid);
				roomList.addRoom(arg[1], roomInfo);
				
				//存储用户操作信息
				UserInfo userInfo = new UserInfo(arg[1]);
				userList.addUser(uid, userInfo);

			}
			
			RoomInfo roomInfo = roomList.getRoom(arg[1]);
			int num = roomInfo.getSize();
			Iterator<String> it = roomInfo.getIterator();
			DMsg<List> dmsg = new DMsg<List>();
			dmsg.setTarget(uid);
			if(num >= 1){
				//发送加入指令
				List<String> uList = new ArrayList<String>();
				while(it.hasNext()){
					String suid = it.next();
					if(!uid.equals(suid)){
						uList.add(suid);
					}
				}
				dmsg.setData(uList);
				dmsg.setCommand("join");
				
			}else{
				//发送等待指令
				dmsg.setCommand("wait");
			}
			msgService.sendToUser(dmsg);
		}
	}

}
